﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace YTSK.Vouch
{
    public class VouchInitGridInventory : DataGridView
    {
        public bool _datagrideditcontrolhasevents;
        protected override void OnEditingControlShowing(DataGridViewEditingControlShowingEventArgs e)
        {
            base.OnEditingControlShowing(e);
            if (!this._datagrideditcontrolhasevents)
            {
                this.EditingControl.Resize += new EventHandler(this.datagridviewEditingControlResize);
                this._datagrideditcontrolhasevents = true;
            }
            datagridviewEditingControlResize(null, null);
        }

        public void LeftEdit()
        {
            var c = this.CurrentCell;
            this.CurrentCell = null;
            this.CurrentCell = c;
        }


        protected void datagridviewEditingControlResize(object sender, EventArgs e)
        {
            object tag = this.Columns[this.CurrentCell.ColumnIndex].Tag;
            Button b;
            Control[] b1 = this.EditingControl.Controls.Find("bref", true);
            if (tag != null && tag.ToString() == "editing")
            {
                if (b1.Length > 0)
                {
                    b = (Button)b1[0];
                }
                else
                {
                    b = new Button();
                    b.Name = "bref";
                    b.Text = "...";
                    b.Font = new System.Drawing.Font("Arial Rounded MT Bold", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    b.Size = new System.Drawing.Size(27, 18);
                    b.BackColor = System.Drawing.SystemColors.Control;
                    b.Click += new System.EventHandler(this.btref_Click);
                    this.EditingControl.Controls.Add(b);
                }
                b.Location = new System.Drawing.Point(this.EditingControl.Width - 25, this.EditingControl.Height - 14);
            }
            else
            {
                if (b1.Length > 0) { this.EditingControl.Controls.Remove(b1[0]); }
            }
        }

        protected void btref_Click(object sender, EventArgs e)
        {
            var val = this.EditingControl.Text.Trim();
            var colname = this.Columns[this.CurrentCell.ColumnIndex].Name;
            switch (colname)
            {
                case "cInvCode":
                    REF.FrmInventory f = new REF.FrmInventory();
                    f.ShowDialog();
                    var inv = f.report.ret;
                    if (inv != null)
                    {
                        this.EditingControl.Text = f.report.ret.cInvCode;
                        var row = this.CurrentRow;
                        if (row != null)
                        {
                            row.Cells["cInvName"].Value = inv.cInvName;
                            row.Cells["cDW"].Value = inv.cDW;
                            row.Cells["cInvName1"].Value = inv.cInvName1;
                        }
                    }
                    f.Close();
                    break;
            }
        }
    }
}
